<!-- 用户列表展示 -->
<template>
  <div class="user-list-container">
    <!-- 搜索区域 -->
    <div class="search-section">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="用户名">
          <el-input
            v-model="searchForm.username"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            v-model="searchForm.phone"
            placeholder="请输入手机号"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" @click="resetSearch"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="operation-section">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd">
        新增用户
      </el-button>
      <el-button
        type="danger"
        icon="el-icon-delete"
        :disabled="!selectedUsers.length"
        @click="handleBatchDelete"
      >
        批量删除
      </el-button>
    </div>

    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      :data="userList"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="username" label="用户名" min-width="120" />
      <el-table-column prop="realName" label="真实姓名" min-width="120" />
      <el-table-column prop="phone" label="手机号" min-width="120" />
      <el-table-column prop="email" label="邮箱" min-width="180" />
      <el-table-column prop="role" label="角色" min-width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.role === 'admin' ? 'danger' : 'success'">
            {{ scope.row.role === "admin" ? "管理员" : "普通用户" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div class="pagination-section">
      <el-pagination
        :current-page="pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: "UserList",
  data() {
    return {
      // 搜索表单
      searchForm: {
        username: "",
        phone: "",
      },
      // 表格数据
      userList: [
        {
          id: 1,
          username: "admin",
          realName: "管理员",
          phone: "13800138000",
          email: "admin@example.com",
          role: "admin",
          status: true,
        },
        {
          id: 2,
          username: "admin",
          realName: "管理员",
          phone: "13800138000",
          email: "admin@example.com",
          role: "admin",
          status: true,
        },
        {
          id: 3,
          username: "admin",
          realName: "管理员",
          phone: "13800138000",
          email: "admin@example.com",
          role: "admin",
          status: true,
        },
        // 更多模拟数据...
      ],
      // 分页参数
      pageNum: 1,
      pageSize: 10,
      total: 0,
      // 加载状态
      loading: false,
      // 选中的用户
      selectedUsers: [],
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    // 获取用户列表
    getUserList() {
      this.loading = true;
      // 模拟接口请求
      setTimeout(() => {
        this.total = 100;
        this.loading = false;
      }, 500);
    },

    // 搜索
    handleSearch() {
      this.pageNum = 1;
      this.getUserList();
    },

    // 重置搜索
    resetSearch() {
      this.searchForm = {
        username: "",
        phone: "",
      };
      this.handleSearch();
    },

    // 新增用户
    handleAdd() {
      this.$router.push("/user/add");
    },

    // 编辑用户
    handleEdit(row) {
      this.$router.push(`/user/update/${row.id}`);
    },

    // 删除用户
    handleDelete(row) {
      this.$confirm("确认删除该用户吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.$message.success("删除成功");
          this.getUserList();
        })
        .catch(() => {});
    },

    // 批量删除
    handleBatchDelete() {
      this.$confirm(
        `确认删除选中的 ${this.selectedUsers.length} 个用户吗？`,
        "提示",
        {
          type: "warning",
        }
      )
        .then(() => {
          this.$message.success("批量删除成功");
          this.getUserList();
        })
        .catch(() => {});
    },

    // 状态改变
    handleStatusChange(row) {
      const status = row.status;
      this.$message.success(
        `已${status ? "启用" : "禁用"}用户：${row.username}`
      );
    },

    // 选择改变
    handleSelectionChange(selection) {
      this.selectedUsers = selection;
    },

    // 每页条数改变
    handleSizeChange(val) {
      this.pageSize = val;
      this.getUserList();
    },

    // 当前页改变
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getUserList();
    },
  },
};
</script>

<style lang="less" scoped>
.user-list-container {
  .search-section {
    background: #fff;
    padding: 18px 20px;
    border-radius: 4px;
    margin-bottom: 16px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

    .search-form {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-wrap: wrap;
      gap: 16px;

      .el-form-item {
        margin-bottom: 0;
      }
    }
  }

  .operation-section {
    margin-bottom: 16px;
    display: flex;
    gap: 10px;
  }

  .el-table {
    margin-bottom: 16px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

    .el-button {
      padding: 4px 8px;

      & + .el-button {
        margin-left: 8px;
      }
    }
  }

  .pagination-section {
    display: flex;
    justify-content: flex-end;
    padding: 16px 0;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  }
}
</style>
